//
//  FilmDetailsViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FilmDetailsViewController.h"
#import "LoginViewController.h"
#import "UIImageView+WebCache.h"
#import "netWorkHandle.h"
#import "Movie.h"
#import <sqlite3.h>

@interface FilmDetailsViewController ()

@end

@implementation FilmDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
  /********************************* 电影详细界面 **************************************/
    
    
    // 左上角返回键
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    // 右上角收藏键
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];

    
    self.navigationItem.title = self.movie.movieName;
    
    // 电影图片
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 130, 190)];
    [imageV sd_setImageWithURLStr:self.movie.image];
    [self.view addSubview:imageV];
    [imageV release];

    // 电影评分
    UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageV.frame.origin.x + imageV.frame.size.width + 25, imageV.frame.origin.y, 100, 25)];
    scoreLabel.text = [NSString stringWithFormat:@"评分 :  %@", self.movie.rating];
    [self.view addSubview:scoreLabel];
    [scoreLabel release];
    
    // 评论人数
    UILabel *ratingCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(scoreLabel.frame.origin.x + scoreLabel.frame.size.width + 5, scoreLabel.frame.origin.y, 200, 25)];
    ratingCountLabel.text = [NSString stringWithFormat:@"(%@评论)", self.movie.rating_count];
    [self.view addSubview:ratingCountLabel];
    [ratingCountLabel release];
    
    // 上映时间
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(scoreLabel.frame.origin.x, scoreLabel.frame.origin.y + 40, 200, 25)];
    dateLabel.text = self.movie.release_date;
    [self.view addSubview:dateLabel];
    [dateLabel release];
    
    // 电影时间
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(scoreLabel.frame.origin.x, dateLabel.frame.origin.y + 40, dateLabel.frame.size.width, dateLabel.frame.size.height)];
    timeLabel.text = self.movie.runtime;
    [self.view addSubview:timeLabel];
    [timeLabel release];
    
    // 电影分类
    UILabel *genresLabel = [[UILabel alloc] initWithFrame:CGRectMake(scoreLabel.frame.origin.x, timeLabel.frame.origin.y + 40, dateLabel.frame.size.width, dateLabel.frame.size.height)];
    genresLabel.text = self.movie.genres;
    [self.view addSubview:genresLabel];
    [genresLabel release];
    
    // 电影国家
    UILabel *countryLabel = [[UILabel alloc] initWithFrame:CGRectMake(scoreLabel.frame.origin.x, genresLabel.frame.origin.y + 37, dateLabel.frame.size.width, dateLabel.frame.size.height)];
    countryLabel.text = self.movie.country;
    [self.view addSubview:countryLabel];
    [countryLabel release];
    
    // 制作人
    UILabel *actorLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageV.frame.origin.x, imageV.frame.origin.y + 10 + imageV.frame.size.height, 100, 30)];
    actorLabel.text = @"制作人";
    actorLabel.font = [UIFont boldSystemFontOfSize:24];
    [self.view addSubview:actorLabel];
    [actorLabel release];
    
    UITextView *actorsLabel =[[UITextView alloc] initWithFrame:CGRectMake(actorLabel.frame.origin.x, actorLabel.frame.origin.y + actorLabel.frame.size.height + 5, [UIScreen mainScreen].bounds.size.width - 40, 65)];
//    actorsLabel.numberOfLines = 0;
    actorsLabel.text = self.movie.actors;
    actorsLabel.font = [UIFont systemFontOfSize:16];
    [actorsLabel setEditable:NO];
    [self.view addSubview:actorsLabel];
    [actorsLabel release];
    
    // 电影情节
    UILabel *plotLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageV.frame.origin.x, actorsLabel.frame.origin.y + actorsLabel.frame.size.height + 10, 100, 30)];
    plotLabel.text = @"电影简介";
    plotLabel.font = [UIFont boldSystemFontOfSize:24];
    [self.view addSubview:plotLabel];
    [plotLabel release];
    
    UITextView *plotText =[[UITextView alloc] initWithFrame:CGRectMake(actorLabel.frame.origin.x, plotLabel.frame.origin.y + plotLabel.frame.size.height + 5, [UIScreen mainScreen].bounds.size.width - 40, 162)];
    //    actorsLabel.numberOfLines = 0;
    plotText.text = self.movie.plot_simple;
    plotText.font = [UIFont systemFontOfSize:16];
    [plotText setEditable:NO];
    [self.view addSubview:plotText];
    [plotText release];

}

- (void)leftAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

// 收藏电影
- (void)rightAction {
    
// 检验有没有登录
    // 打开数据库
    static sqlite3 *DB = nil;
    NSString *dbPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [dbPath stringByAppendingPathComponent:@"dataBase.db"];
    sqlite3_open(filePath.UTF8String, &DB);
    
    // 查询有没有状态为登录("T")
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM user WHERE  userState LIKE '%%%@%%'", @"T"];
    sqlite3_stmt *stmt = nil;
    sqlite3_prepare(DB, sqlStr.UTF8String, -1, &stmt, NULL);
    
    NSString *name = nil;
    
    while (SQLITE_ROW == sqlite3_step(stmt)) {
        
        const unsigned char *userName = sqlite3_column_text(stmt, 1);
        name = [NSString stringWithUTF8String:(const char *)userName];
    }
    
    if (name == nil) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"还没有登录,是否登录? " preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
            [loginVC release];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            return;
        }];
        
        [alert addAction:yesAction];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }
    else {
        
        // 查找有没有收藏过该活动
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM filmTable WHERE  userName LIKE '%%%@%%' and movieName LIKE '%%%@%%'", name, self.movie.movieName];
        sqlite3_stmt *stmt = nil;
        sqlite3_prepare(DB, sqlStr.UTF8String, -1, &stmt, NULL);
        NSString *movie = nil;
        
        while (SQLITE_ROW == sqlite3_step(stmt)) {
            
            const unsigned char *title = sqlite3_column_text(stmt, 2);
            movie = [NSString stringWithUTF8String:(const char *)title];
        }
        if (nil == movie) {

            // 插入电影收藏
            NSString *sqlStr = [NSString stringWithFormat:@"INSERT INTO filmTable(userName, movieName, image, plot_simple, genres, country, runtime, poster, rating_count, rating, release_date, actors) VALUES ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", name, self.movie.movieName, self.movie.image, self.movie.plot_simple, self.movie.genres, self.movie.country, self.movie.runtime, nil, self.movie.rating_count, self.movie.rating, self.movie.release_date, self.movie.actors];
            sqlite3_exec(DB, sqlStr.UTF8String, NULL, NULL, NULL);
            
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"收藏成功啦" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
            
            [alter show];
            [alter release];
            return;
            
        }
        else {
            
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"已经收藏过啦" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
            
            [alter show];
            [alter release];
            return;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
